package com.lg.aptcomp.model


import androidx.databinding.BaseObservable
import com.lg.aptcomp.R
import com.lg.aptcomp.SecondFragment


class LoginFormModel : BaseObservable() {

    var username: String = ""
    var email: String = ""
    var mobile: String = ""
    var password: String = ""

    fun isFormValidated(fragmentContext: SecondFragment): Boolean {
        var isFormValidated = true

        /* Checking Password */
        if (password.trim().isBlank()) {
            isFormValidated = false
            fragmentContext.mContentViewBinding.passwordTil.error = fragmentContext.getString(R.string.password) + " " + fragmentContext.getString(R.string.is_required)
//            showShakeError(fragmentContext.context!!, fragmentContext.mContentViewBinding.passwordTil)
            fragmentContext.mContentViewBinding.passwordTil.requestFocus()
        } else if (password.trim().length < 6) {
            isFormValidated = false
            fragmentContext.mContentViewBinding.passwordTil.error = fragmentContext.getString(R.string.enter_a_valid) + " " + fragmentContext.getString(R.string.password)
            //showShakeError(fragmentContext.context!!, fragmentContext.mContentViewBinding.passwordTil)
            fragmentContext.mContentViewBinding.passwordTil.requestFocus()
        } else {
            fragmentContext.mContentViewBinding.passwordTil.isErrorEnabled = false
            fragmentContext.mContentViewBinding.passwordTil.error = null
        }
//
//        /* Checking Username */
        if (username.trim().isBlank()) {
            isFormValidated = false
            fragmentContext.mContentViewBinding.emailTil.error = fragmentContext.getString(R.string.username) + " " + fragmentContext.getString(R.string.is_required)
//            showShakeError(fragmentContext.context!!, fragmentContext.mContentViewBinding.emailTil)
            fragmentContext.mContentViewBinding.emailTil.requestFocus()
        }  else {
            fragmentContext.mContentViewBinding.emailTil.isErrorEnabled = false
            fragmentContext.mContentViewBinding.emailTil.error = null

            if (android.util.Patterns.EMAIL_ADDRESS.matcher(username.trim()).matches()) {
                email = username
                mobile = ""
            } else if (android.util.Patterns.PHONE.matcher(username.trim()).matches()) {
                email = ""
                mobile = username
            }
        }
        return isFormValidated
    }


}